package org.hertsig.legality;

import java.util.List;
import java.util.Set;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Multiset;

/**
 * 
 */
class CombinedLegalityChecker extends LegalityChecker {
	private final ImmutableList<LegalityChecker> checkers;
	
	protected CombinedLegalityChecker(String name, LegalityChecker... checkers) {
		super(name, ImmutableSet.<String>of(), ImmutableSet.<String>of());
		this.checkers = ImmutableList.copyOf(checkers);
	}
	
	@Override
	protected void check(Multiset<String> cards, ImmutableSet.Builder<Conflict> builder) {
		List<Set<LegalityChecker.Conflict>> conflicts = Lists.newArrayList();
		for (LegalityChecker checker : checkers) {
			Builder<LegalityChecker.Conflict> subBuilder = ImmutableSet.builder();
			checker.check(cards, subBuilder);
			ImmutableSet<Conflict> subConflicts = subBuilder.build();
			if (subConflicts.isEmpty()) {
				return;
			}
			conflicts.add(subConflicts);
		}
		builder.addAll(Iterables.concat(conflicts));
	}
}
